package com.wasabiway.livewallpaper.starsinthesky;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import com.example.android.apis.graphics.ColorPickerDialog;

public class StarsInTheSkySettings extends PreferenceActivity
    implements SharedPreferences.OnSharedPreferenceChangeListener, 
    				ColorPickerDialog.OnColorChangedListener {
	
    private CheckBoxPreference stars, comets, supernovae;
    private EditTextPreference starsNumber;
    private Preference starsColor, cometsColor, supernovaeColor;
    private Paint starColor, cometColor, supernovaColor;
	private Editor editor;
    private int colorOption = 0;
    private static final String TAG = "StarsInTheSkySettings";
	
	@Override
    protected void onCreate(Bundle icicle) {
		CelestialConstants.logging(TAG);
        super.onCreate(icicle);
        getPreferenceManager().setSharedPreferencesName(
        		StarsInTheSky.SHARED_PREFS_NAME);
        addPreferencesFromResource(R.layout.starsinthesky_settings);
        getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(
                this);
        
        starColor = new Paint();
        cometColor = new Paint();
        supernovaColor = new Paint();
        editor = getPreferenceScreen().getEditor();
        
        setStarsSettings();
        setCometsSettings();
        setSupernovaeSettings();
    }

    @Override
    protected void onResume() {
    	CelestialConstants.logging(TAG);
        super.onResume();
    }

    @Override
    protected void onDestroy() {
    	CelestialConstants.logging(TAG);
        getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(
                this);
        super.onDestroy();
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    	CelestialConstants.logging(TAG);
    }
    
    public void setStarsSettings(){
    	CelestialConstants.logging(TAG);
        stars = (CheckBoxPreference) findPreference(CelestialConstants.SET_STARS);
        starsNumber = (EditTextPreference) findPreference(CelestialConstants.SET_STARSNUM);
        starsColor = (Preference) findPreference(CelestialConstants.SET_STARSCOLOR);
        
        starsColor.setOnPreferenceClickListener(new ColorSettingsClick());
    }
    
    public void setCometsSettings(){
    	CelestialConstants.logging(TAG);
        comets = (CheckBoxPreference) findPreference(CelestialConstants.SET_COMETS);
        cometsColor = (Preference) findPreference(CelestialConstants.SET_COMETSCOLOR);
        
        cometsColor.setOnPreferenceClickListener(new ColorSettingsClick());
    }
    
    public void setSupernovaeSettings(){
    	CelestialConstants.logging(TAG);
        supernovae = (CheckBoxPreference) findPreference(CelestialConstants.SET_SUPERNOVAE);
        supernovaeColor = (Preference) findPreference(CelestialConstants.SET_SUPERNOVAECOLOR);
        
        supernovaeColor.setOnPreferenceClickListener(new ColorSettingsClick());
    }
    
    /**
     * Changes the color of the correct Paint object
     * @param color
     */
	@Override
	public void colorChanged(int color){
		CelestialConstants.logging(TAG);
		// Sets the color value of what's changed
		switch(this.colorOption){
			case CelestialConstants.STARS_COLOR:
				editor.putString(CelestialConstants.SET_STARSCOLOR, "#"+Integer.toHexString(color));
				break;
			case CelestialConstants.COMETS_COLOR:
				editor.putString(CelestialConstants.SET_COMETSCOLOR, "#"+Integer.toHexString(color));
				break;
			case CelestialConstants.SUPERNOVAE_COLOR:
				editor.	putString(CelestialConstants.SET_SUPERNOVAECOLOR, "#"+Integer.toHexString(color));
				break;
			default: break;
		}
		// Commits the change
		// and resets what the colorOption value is
		editor.commit();
		this.colorOption = 0;
	}
	
    /**
     * When a Color Settings is changed, the colorOption is set accordingly
     * A ColorPickerDialog appears for the correct Paint object
     * @param colour
     */
    private void colorChange(int colorOption){
    	CelestialConstants.logging(TAG);
        starColor.setColor(Color.parseColor(starsColor.getSharedPreferences().getString(CelestialConstants.SET_STARSCOLOR, "#FFFFFFFF")));
        cometColor.setColor(Color.parseColor(cometsColor.getSharedPreferences().getString(CelestialConstants.SET_COMETSCOLOR, "#FFFFFFFF")));
        supernovaColor.setColor(Color.parseColor(supernovaeColor.getSharedPreferences().getString(CelestialConstants.SET_SUPERNOVAECOLOR, "#FFFFFFFF")));
        
        this.colorOption = colorOption;
        switch(colorOption){
        	case 1:
    	    	new ColorPickerDialog(this, this, starColor.getColor()).show();
    	    	break;
        	case 2:
    	    	new ColorPickerDialog(this, this, cometColor.getColor()).show();
    	    	break;
        	case 3:
    	    	new ColorPickerDialog(this, this, supernovaColor.getColor()).show();
    	    	break;
    		default: break;
        }
    }
    
    /**
     * This class takes care of click events for the Color Settings
     * 
     * @author Ewei
     *
     */
    public class ColorSettingsClick implements Preference.OnPreferenceClickListener{
    	private static final String TAG = "ColorSettingsClick";
    	/**
    	 * Differentiates what is clicked on
    	 */
		@Override
		public boolean onPreferenceClick(Preference preference){
			CelestialConstants.logging(TAG);
			
			if(preference.equals(starsColor))
				colorChange(CelestialConstants.STARS_COLOR);
			if(preference.equals(cometsColor))
				colorChange(CelestialConstants.COMETS_COLOR);
			if(preference.equals(supernovaeColor))
				colorChange(CelestialConstants.SUPERNOVAE_COLOR);
			return false;
		}
    }
}
